#include <mips/regdef.h>
#ifndef 	fp
#define	fp	$fp
#endif
#define 	SSIZE	32
#define		O_RA	24
#define		O_FP	20
#define		O_GP	16
#define		O_ARG1	SSIZE
	.text
	.align 	2
	.globl	myatoi
	.ent	myatoi
myatoi:	.frame fp,SSIZE,ra
		.set noreorder
		.cpload t9
		.set reorder
		subu	sp,sp,SSIZE
		sw		gp,O_GP(sp)
		sw		ra,O_RA(sp)
		sw		fp,O_FP(sp)
		move	fp,sp
		sw		a0,O_ARG1(fp)
		lw		a0,O_ARG1(fp)
# t0 original
# t1 salida
# t2 indice
# t3 caracter leido
# t4 auxiliar lectura
# t5 10
# t6 auxiliar comparacion
L0:		addu	t0,a0,zero #original=a0
		addu	t1,zero,zero #salida=0
		addu	t2,zero,zero #indice=0
		addiu	t5,zero,10
L1:		addu	t4,t0,t2
		lb		t3,0(t4) #leo caracter
		ble		t3,zero,fin_myatoi #salto si cero
		mult    t1,t5
		mflo	t1
		#obtención de valor byte
		# 0
		addiu	t6,zero,48
		bne		t3,t6,L2
		addu	t1,t1,0
		b		L11
		# 1
L2:		addiu	t6,t6,1
		bne		t3,t6,L3
		addu	t1,t1,1
		b		L11
		# 2
L3:		addiu	t6,t6,1
		bne		t3,t6,L4
		addu	t1,t1,2
		b		L11
		# 3
L4:		addiu	t6,t6,1
		bne		t3,t6,L5
		addu	t1,t1,3
		b		L11
		# 4
L5:		addiu	t6,t6,1
		bne		t3,t6,L6
		addu	t1,t1,4
		b		L11
		# 5
L6:		addiu	t6,t6,1
		bne		t3,t6,L7
		addu	t1,t1,5
		b		L11
		# 6
L7:		addiu	t6,t6,1
		bne		t3,t6,L8
		addu	t1,t1,6
		b		L11
		# 7
L8:		addiu	t6,t6,1
		bne		t3,t6,L9
		addu	t1,t1,7
		b		L11
		# 8
L9:		addiu	t6,t6,1
		bne		t3,t6,L10
		addu	t1,t1,8
		b		L11
		# 9
L10:	addiu	t6,t6,1
		bne		t3,t6,L11
		addu	t1,t1,9
		b		L11
L11:
		addiu	t2,t2,1 #incremento indice
		b 		L1
fin_myatoi:
		addu	v0,t1,zero
		move	sp,fp
		lw		a0,O_ARG1(sp)
		lw		gp,O_GP(sp)
		lw		ra,O_RA(sp)
		lw		fp,O_FP(sp)
		addiu	sp,sp,SSIZE
		j		ra
		.end myatoi
